package com.mengsenlin.controller;

import com.mengsenlin.entity.*;
import com.mengsenlin.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/7/16.
 */
@Controller
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "/home")
    public String home(Model model) {
        List<PollEntity> pollGoods = goodsService.selectPollGoods();
        model.addAttribute("pollGoods", pollGoods);
        List<RecommendEntity> recommendGoods = goodsService.selectRecommendGoods();
        model.addAttribute("recommendGoods", recommendGoods);
//        Goods topGoods = goodsService.selectTopGoods();
//        model.addAttribute("topGoods",topGoods);
//        Goods recommendGoods = goodsService.selectRecommendGoods();
        return "home";
    }

    @RequestMapping(value = "/home/top", method = RequestMethod.GET)
    @ResponseBody
    public List<TopEntity> homeTop(Model model) {
        return goodsService.selectTopGoods();
    }

    @RequestMapping(value = "/home/spe", method = RequestMethod.GET)
    @ResponseBody
    public List<GoodsSpecification> homeSpe(@RequestParam("goodsId") long goodsId) {
        return goodsService.selectGoodsSpe(goodsId);
    }

    @RequestMapping(value = "/cart", method = RequestMethod.GET)
    public String cart(Model model, HttpSession session){

        List<CartItem> cartItems = goodsService.selectCartGoods(session);
        model.addAttribute("cartItems",cartItems);
        return "cart";
    }

    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public String detail(@RequestParam("goodsId")long goodsId, Model model){
        List<DetailGoods> detailGoods = goodsService.selectDetailGoods(goodsId);
        model.addAttribute("detailGoods",detailGoods);
        DetailGoods url = goodsService.selectUrlDetailGoods(goodsId);
        model.addAttribute("url",url);
        return "detail";
    }

}